<template xlang="wxml">
	<view class="tki-tree lsitPages">
		<!-- 	<view class="tki-tree-mask" :class="{'show':showTree}" @tap="_cancel"></view>
		<view class="tki-tree-cnt" :class="{'show':showTree}"> -->

		<view class="tki-tree-view">
			<scroll-view class="tki-tree-view-sc" :scroll-y="true">
				<block style="background-color: #fff;" v-for="(item, index) in treeList" :key="index">
					<!-- height:isShow? 580+'upx' :'180upx' -->
					<view class="tki-tree-item" :style="[{
							paddingLeft: item.rank*15 + 'px',
							zIndex: item.rank*-1 +50,
							height:item.isShows?370+'upx' :'268upx'
							
						}]" :class="{
							border: border === true,
							show: item.show,
							last: item.lastRank,
							showchild: item.showChild,
							open: item.open,
						}">
						<!-- <view style="display: flex;align-items: center;"> -->
						<!-- <view class="card-view retract icon" @click="test(item)">
								<text class="iconfont next icon-fanhui" :class="[item.isShows ? '' : 'retract_icon']" />
							</view> -->

						<view style="width: 100%;" class="contentInfo">
							<view class="tki-tree-label titleRate" @tap.stop="_treeItemTap(item, index)">
								<!-- <image class="tki-tree-icon" :src="item.lastRank ? lastIcon : item.showChild ? currentIcon : defaultIcon"></image> -->
								<view class="text_blue">
									<view>({{index+0}}) 被检查试验室 <text class="name">{{item.fCheckcompanyname}}</text>
									</view>
									<!-- 						<view v-else>({{index+0}}) 备案编号 <text class="name">{{item.fBackupnumber}}</text>
									</view> -->
									<!--  -->
									<view class="tki-tree-check" @tap.stop="_treeItemSelect(item, index)"
										v-if="selectParent?true:item.lastRank">
										<view class="tki-tree-check-yes" v-if="item.checked"
											:class="{'radio':!multiple}" :style="{'border-color':confirmColor}">
											<view class="tki-tree-check-yes-b"
												:style="{'background-color':confirmColor}"></view>
										</view>
										<view class="tki-tree-check-no" v-else :class="{'radio':!multiple}"
											:style="{'border-color':confirmColor}"></view>
									</view>
								</view>
							</view>
							<view class="iconSelect">
								<view class="iconSelectLeft">
									<view class="tki-tree-label tki-tree-line" style="">检测方式 <text
											class="name">{{item.fSigncompanyname}}</text>
									</view>

									<!-- fBackupnumber -->
									<view class="tki-tree-label tki-tree-line" style="">检查时间 <text
											class="name">{{item.fWorkdates}}</text></view>
									<view class="tki-tree-label tki-tree-line" style="">检查负责人 <text
											class="name">{{item.fLeadername}}</text></view>
									<view class="tki-tree-label tki-tree-line" style="">状态 <text
											class="name">{{item.fStatus}}</text></view>

								</view>
								<!-- 	<view class="card-view retract icon" @click="test(item,index)">
									<text class="iconfont next icon-fanhui"
										:class="[item.isShows ? '' : 'retract_icon']" />
								</view> -->
							</view>
							<!-- <view class="min-collapse">
								<view class="card">
									<view>
										<view class="contents"
											:class="item.isShows ? 'animation': 'show-list animation' "
											:style="{'height':item.isShows? 150+'upx' :'0'}">
											<view class="projectInfo ">
												<view class="iconSelectLeft">
													<view class="tki-tree-label tki-tree-line"
														style="border-bottom: 1upx solid #eee;border-top: 1upx solid #eee;">
														项目地点 <text class="name"
															style="margin-left: 28upx;">{{item.companyName}}</text>
													</view>
													<view class="tki-tree-label tki-tree-line">委托单位 <text class="name"
															style="margin-left: 28upx;">{{item.companyName}}</text>
													</view>

												</view>
												
											</view>

										</view>
									</view>
								</view>

							</view> -->

						</view>

					</view>



				</block>
			</scroll-view>
		</view>
		<view class="tki-tree-bar">
			<!-- <view class="tki-tree-bar-cancel" :style="{'color':cancelColor}" hover-class="hover-c" @tap="_cancel">取消
			</view> -->
			<!-- <view class="tki-tree-bar-title" :style="{'color':titleColor}">{{title}}</view> -->
			<view class="tki-tree-bar-confirm" :style="{'color':confirmColor}" hover-class="hover-c" @tap="_confirm">
				确认选择
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	// import t-table
	import tTable from '@/components/t-table/t-table.vue';
	import tTh from '@/components/t-table/t-th.vue';
	import tTr from '@/components/t-table/t-tr.vue';
	import tTd from '@/components/t-table/t-td.vue';
	export default {
		name: "tki-tree",
		props: {
			range: {
				type: Array,
				default: function() {
					return []
				}
			},
			fBackupnumber: {
				type: String,
			},

			idKey: {
				type: String,
				default: 'id'
			},
			fStatus: {
				type: String,
				// default: 'label'
			},
			fLeadername: {
				type: String,
				// default: 'label'
			},
			fWorkdates: {
				type: String,
				// default: 'label'
			},
			fSigncompanyname: {
				type: String,
				// default: 'label'
			},
			fCheckcompanyname: {
				type: String,
				// default: 'label'
			},

			// hostusertId: {
			// 	type: String,

			// },
			fStone1: {
				type: String,
				// default: 'label'
			},
			fStone2: {
				type: String,
				// default: 'label'
			},
			fStone3: {
				type: String,
				// default: 'label'
			},
			fWater: {
				type: String,
				// default: 'label'
			},
			fWaterreducer: {
				type: String,
				// default: 'label'
			},
			fFlyash: {
				type: String,
				// default: 'label'
			},
			fSlagpowder: {
				type: String,
				// default: 'label'
			},
			fWaterproof: {
				type: String,
				// default: 'label'
			},
			fId: {
				type: String,
			},
			projectType: {
				type: String,
				// default: 'label'
			},
			isShowS: "",
			projectName: {
				type: String,
				// default: 'label'
			},
			rangeKey: {
				type: String,
				default: 'label'
			},

			title: {
				type: String,
				default: ''
			},
			multiple: { // 是否可以多选
				type: Boolean,
				default: false
				// default: true
			},
			selectParent: { //是否可以选父级
				type: Boolean,
				default: false
			},
			foldAll: { //折叠时关闭所有已经打开的子集，再次打开时需要一级一级打开
				type: Boolean,
				default: false
			},
			confirmColor: { // 确定按钮颜色
				type: String,
				default: '' // #1752db
			},
			cancelColor: { // 取消按钮颜色
				type: String,
				default: '' // #757575
			},
			titleColor: { // 标题颜色
				type: String,
				default: '' // #1752db
			},
			currentIcon: { // 展开时候的ic
				type: String,
				default: ''
			},
			defaultIcon: { // 折叠时候的ic
				type: String,
				default: ''
			},
			lastIcon: { // 没有子集的ic
				type: String,
				default: ''
			},
			border: { // 是否有分割线
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				tableList: [{
						id: 0,
						name: '张三',
						age: '19',
						hobby: '游泳'
					},
					{
						id: 1,
						name: '李四',
						age: '21',
						hobby: '绘画'
					},
					{
						id: 2,
						name: '王二',
						age: '29',
						hobby: '滑板'
					},
					{
						id: 3,
						name: '码字',
						age: '20',
						hobby: '蹦极'
					}
				],
				currentActive: 0,
				showTree: false,
				treeList: [],
				selectIndex: -1,
				isShow: false,
				currents: Number,
				values: '',
				show: false,
			}
		},
		computed: {},
		components: {
			tTable,
			tTh,
			tTr,
			tTd
		},
		methods: {
			test(item, index) {
				// console.log(item)
				item.isShows = !item.isShows
				// console.log(item.isShows)
				this.currentActive = index
				// this.isShow = !this.isShow
			},

			_show() {
				this.showTree = true
			},
			_hide() {
				this.showTree = false
			},
			_cancel() {
				this._hide()
				this.$emit("cancel", '');
			},
			_confirm() {
				// 处理所选数据
				let rt = [],
					obj = {};
				this.treeList.forEach((v, i) => {
					if (this.treeList[i].checked) {
						obj = {}
						obj.parents = this.treeList[i].parents
						obj = Object.assign(obj, this.treeList[i].source)
						// 移除子元素
						delete obj.children
						rt.push(obj)
					}
				})
				this._hide()
				this.$emit("confirm", rt);
			},
			//扁平化树结构
			_renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
				console.log(list)
				list.forEach(item => {
					this.treeList.push({
						id: item[this.idKey],
						name: item[this.rangeKey],
						projectName: item[this.projectName],
						fBackupnumber: item[this.fBackupnumber],
						projectType: item[this.projectType],
						isShows: item[this.isShowS],
						fId: item[this.fId],
						fStatus: item[this.fStatus],
						fSigncompanyname: item[this.fSigncompanyname], //检查方式--
						fWorkdates: item[this.fWorkdates], //检查方式--
						fLeadername: item[this.fLeadername], //检查方式--
						fCheckcompanyname: item[this.fCheckcompanyname], //检查方式--




						// hostusertId: item[this.hostusertId],

						source: item,
						parentId, // 父级id数组
						parents, // 父级id数组
						rank, // 层级
						showChild: false, //子级是否显示
						open: false, //是否打开
						show: rank === 0, // 自身是否显示
						hideArr: [],
						orChecked: item.checked ? item.checked : false,
						checked: item.checked ? item.checked : false,
					})
					if (Array.isArray(item.children) && item.children.length > 0) {
						// console.log(item)
						let parentid = [...parentId],
							parentArr = [...parents],
							childrenid = [...childrenid];
						delete parentArr.children
						parentid.push(item[this.idKey]);
						parentArr.push({
							[this.idKey]: item[this.idKey],
							[this.rangeKey]: item[this.rangeKey]
						})
						this._renderTreeList(item.children, rank + 1, parentid, parentArr);
					} else {
						this.treeList[this.treeList.length - 1].lastRank = true;
					}
				})
				// console.log(list)
			},
			// 处理默认选择
			_defaultSelect() {
				this.treeList.forEach((v, i) => {
					if (v.checked) {
						this.treeList.forEach((v2, i2) => {
							if (v.parentId.toString().indexOf(v2.parentId.toString()) >= 0) {
								v2.show = true
								if (v.parentId.includes(v2.id)) {
									v2.showChild = true;
									v2.open = true;
								}
							}
						})
					}
				})
			},
			// 点击
			_treeItemTap(item, index) {
				if (item.lastRank === true) {
					//点击最后一级时触发事件
					this.treeList[index].checked = !this.treeList[index].checked
					this._fixMultiple(index)
					return;
				}
				let list = this.treeList;
				let id = item.id;
				item.showChild = !item.showChild;
				item.open = item.showChild ? true : !item.open;
				list.forEach((childItem, i) => {
					if (item.showChild === false) {
						//隐藏所有子级
						if (!childItem.parentId.includes(id)) {
							return;
						}
						if (!this.foldAll) {
							if (childItem.lastRank !== true && !childItem.open) {
								childItem.showChild = false;
							}
							// 为隐藏的内容添加一个标记
							if (childItem.show) {
								childItem.hideArr[item.rank] = id
							}
						} else {
							if (childItem.lastRank !== true) {
								childItem.showChild = false;
							}
						}
						childItem.show = false;
					} else {
						// 打开子集
						if (childItem.parentId[childItem.parentId.length - 1] === id) {
							childItem.show = true;
						}
						// 打开被隐藏的子集
						if (childItem.parentId.includes(id) && !this.foldAll) {
							// console.log(childItem.hideArr)
							if (childItem.hideArr[item.rank] === id) {
								childItem.show = true;
								if (childItem.open && childItem.showChild) {
									childItem.showChild = true
								} else {
									childItem.showChild = false
								}
								childItem.hideArr[item.rank] = null
							}
							// console.log(childItem.hideArr)
						}
					}
				})
				// console.log(this.treeList)
			},
			_treeItemSelect(item, index) {
				this.treeList[index].checked = !this.treeList[index].checked
				this._fixMultiple(index)
			},
			// 处理单选多选
			_fixMultiple(index) {
				if (!this.multiple) {
					// 如果是单选
					this.treeList.forEach((v, i) => {
						if (i != index) {
							this.treeList[i].checked = false
						} else {
							this.treeList[i].checked = true
						}
					})
				}
			},
			// 重置数据
			_reTreeList() {
				this.treeList.forEach((v, i) => {
					this.treeList[i].checked = v.orChecked
				})
			},
			_initTree(range = this.range) {
				// console.log(this.range,range)
				this.treeList = [];
				this._renderTreeList(range);
				this.$nextTick(() => {
					this._defaultSelect(range)
				})
			}
		},
		watch: {
			range(list) {
				// console.log(list)
				this._initTree(list);
			},
			multiple() {
				if (this.range.length) {
					this._reTreeList();
				}
			},
			selectParent() {
				if (this.range.length) {
					this._reTreeList();
				}
			},
		},
		mounted() {
			this._initTree();
		}
	}
</script>

<style scoped lang="less">
	@import "./style.css";
	@import "./icon.css";
	@import (less) '../../static/lx-calendar/fonts/iconfont.css';

	.min-collapse {
		width: 100%;
		background-color: #fff;
		// padding: 0 20upx 0;
		font-size: 26upx;
		line-height: 2.2 !important;
		// margin-top: 30upx;

		// padding:  0px 25px 10px !important;

		.card {
			width: 100%;
		}

		.tki-tree-labels {
			width: 100%;
			// line-height: 1.8;
			height: 50upx;
			color: rgb(117, 117, 117);
		}

		.right-arrow {
			//p-left-10
			width: 24rpx;
			height: 24rpx;
			padding-top: 10rpx;
			transform: rotate(90deg);
			padding-left: 10rpx;
		}

		.right-arrow-a {
			transform: rotate(7deg);
		}

		.card {
			// box-shadow: 0 0 10px 4px #EEEEEE;

			.card-view {
				width: 100%;
				//min-flex min-flex-main-between f28
				display: flex;
				justify-content: center;
				font-size: 28rpx;
				padding: 0rpx;
				padding-bottom: 0;

			}
		}

		.contents {
			overflow: hidden;
			display: flex;
			// justify-content: flex-start;
			// flex-wrap: wrap;
			// max-height: 0;
			// padding: 0 20rpx;


			.chioce-date-item {
				// width: 112rpx;
				// height: 58rpx;


				font-size: 28rpx;
				text-align: center;

				display: block;
			}

			.chioce-date-item:nth-child(5n) {
				margin: 0;
			}

			.chioce-date-item-active {
				// width: 112rpx;
				// height: 58rpx;
				font-size: 28rpx;
				text-align: center;
			}

			.chioce-date-item-active:nth-child(5n) {
				margin: 0;
			}

			.m-right-20 {
				// margin-right: 35rpx;
				// margin-bottom: 20rpx;
			}
		}

		//在txt中修改左右间距，默认使用父组件外容器的边距
		.content-txt {
			overflow: hidden;
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;
			max-height: 0;
			padding-left: 20rpx;
			padding-right: 20rpx;

			// padding-bottom: 20rpx;
			.txt {
				word-break: break-all;
				white-space: normal;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				overflow: hidden;
				// padding:0 20rpx;
				padding-bottom: 20rpx;
				font-size: 28rpx;
				font-weight: normal;
			}
		}

		.show-list {
			height: 0;
		}

		.show {
			max-height: 0;
			margin-bottom: 20rpx;
		}

		.animation {
			transition-property: all;
			transition-duration: 0.5s;
			transition-timing-function: ease;
		}
	}

	.icon-fanhui::before {
		content: "";
		color: #A7A7A7;
		font-weight: 400;
		right: 25%;
		top: 3%;
		position: absolute;
		width: 18.48upx;
		height: 11.2upx;


	}

	.icon-fanhui::after {
		width: 18.48upx;
		height: 11.2upx;
		content: "";
		// position: absolute;
		color: #A7A7A7;
		// right: 50upx;
	}

	.icon {
		display: block;

		.next {
			transform: rotate(90deg);
			display: block;
		}
	}

	.retract {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 80rpx;

		.iconfont {
			// transform: rotate(180deg);

			&.retract_icon {
				transform: rotate(270deg);
			}
		}
	}
	.text_blue .name {
		display: inline-block;
		margin-left: 25upx;
		 color: #62A7FC; 
		font-size: 28upx;
		font-weight: 700;
	}
	.tki-tree-check-no {
		width: 20px;
		height: 20px;
		border-top-left-radius: 20%;
		border-top-right-radius: 20%;
		border-bottom-right-radius: 20%;
		border-bottom-left-radius: 20%;
		border-top-width: 1rpx;
		border-left-width: 1rpx;
		border-bottom-width: 1rpx;
		border-right-width: 1rpx;
		border-style: solid;
		border-color: #2B85FF;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
	}
</style>
